package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3RecongitionWarnPhotoAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.alone.PictureRecogintionResultBean;
import com.das.mechanic_base.bean.main.CarDetectionBean;
import com.das.mechanic_base.bean.main.DetectionBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.view.View.GONE;

public class X3BottomHomeAloneCarDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3BottomHomeTyreDialog.IOnRefresh, X3BottomHomeCarPaintDialog.IOnRefresh, X3BottomAloneThingDialog.IOnRefresh {

    private Button btn_next;
    private TextView tv_cancel;
    private TextView tv_clear;
    protected int carId;
    private LinearLayout ll_content;

    private HashMap<String, CarDetectionBean> carMap;

    private String rightQuestionSystemSn;
    private String appearanceQuestionSystemSn;
    /**
     * 进厂油量
     */
    private String oilContent;

    private X3BottomHomeCarOwnerTextDialog carOwnerTextDialog;
    private List<String> oilList;
    private int receiveBaseId;
    private X3DashboardWarningLightsDialog dashboardWarningLightsDialog;
    public static boolean isSuccessLight=true;
    public static boolean isSuccessAppearance=true;
    private Handler handler =new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (!X3StringUtils.isEmpty(appearanceQuestionSystemSn)) {//外观损伤
                getrecognitionStatus(appearanceQuestionSystemSn, receiveBaseId);
            }
            if (!X3StringUtils.isEmpty(rightQuestionSystemSn)) {//故障灯
                getrecognitionStatus(rightQuestionSystemSn, receiveBaseId);
            }
            handler.postDelayed(this, 1000);
        }
    };
    private X3AppearanceamageDialog appearanceamageDialog;
    private List<List<DetectionBean>> mLists;


    public X3BottomHomeAloneCarDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }


    @Override
    protected void initView() {
        btn_next = getView(R.id.btn_next);
        tv_cancel = getView(R.id.tv_cancel);
        tv_clear = getView(R.id.tv_clear);
        ll_content = getView(R.id.ll_content);

        oilList = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            oilList.add(i * 10 + "");
        }

        btn_next.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_clear.setOnClickListener(this);

        handler.postDelayed(runnable,1000);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_car_alone_bottom_dialog;
    }

    @Override
    public void show() {
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        super.show();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(String content) {
        if (X3StringUtils.isEmpty(content)) {
            return;
        }
        if(content.equals("UPDATE_PRELIMINARY_INSPECTION_INFORMATION")){
            System.out.println("接收到刷新的通知了");
            requestDetection(receiveBaseId,carId,false,false);
        }else if(content.equals("REFRESHLIGHT")){
            System.out.println("图片开始上传了 展示loading 40s");
            if (X3StringUtils.isListEmpty(mLists)) {
                return;
            }
            showViewList(mLists,true,false);
        }else if(content.equals("REFRESHAPPEARANCE")){
            if (X3StringUtils.isListEmpty(mLists)) {
                return;
            }
            showViewList(mLists,false,true);
        }
    }
    /**
     * 请求初检信息
     * @param receiveBaseId
     */

    public void requestDetection(int receiveBaseId,int carId,boolean isShowLoadinglight,boolean isShowLoadingAppearance) {
        this.receiveBaseId = receiveBaseId;
        this.carId=carId;
        NetWorkHttp.getApi().getDetectionFirstInfo(receiveBaseId,true)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<List<DetectionBean>>>() {
                    @Override
                    protected void onDone(List<List<DetectionBean>> mList) {
                        if (X3StringUtils.isListEmpty(mList)) {
                            return;
                        }
                        mLists = mList;
                        showViewList(mList,isShowLoadinglight,isShowLoadingAppearance);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }
    /*
    *初检车辆图片识别状态查询
    */
    public void getrecognitionStatus(String questionToSystemSn,int receiveBaseId) {
        this.receiveBaseId = receiveBaseId;
        NetWorkHttp.getApi().getrecognitionStatus(questionToSystemSn,receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<PictureRecogintionResultBean>() {
                    @Override
                    protected void onDone(PictureRecogintionResultBean pictureRecogintionResultBean) {
                        System.out.println("初检车辆图片识别状态查询");
                        if (pictureRecogintionResultBean == null) {
                            return;
                        }

                        long finishAmount = pictureRecogintionResultBean.finishAmount;
                        long totalAmount = pictureRecogintionResultBean.totalAmount;

                        if (totalAmount < 0) {
                            return;
                        }



                        if (totalAmount == 0) {
                            return;
                        }

                        if (questionToSystemSn == rightQuestionSystemSn && finishAmount != totalAmount) {//故障灯
                            // 请求中
                            isSuccessLight=true;
//                            requestDetection(receiveBaseId,carId,true,false);
                            System.out.println("故障灯数据不相等");

                        }else if(questionToSystemSn == appearanceQuestionSystemSn && finishAmount != totalAmount) {//外观损伤
                            isSuccessAppearance=true;
//                            requestDetection(receiveBaseId,carId,false,true);
                            System.out.println("车损外观数据不相等");
                        }

                        if ( questionToSystemSn == rightQuestionSystemSn && finishAmount == totalAmount && isSuccessLight) {
                            isSuccessLight=false;
                            // 完成
                            //刷新初检信息接口
                            requestDetection(receiveBaseId,carId,false,false);
                            System.out.println("故障灯数据相等");
                            if(questionToSystemSn == rightQuestionSystemSn && dashboardWarningLightsDialog != null && dashboardWarningLightsDialog.isShowing()){//故障灯
                                dashboardWarningLightsDialog.setDetailSystemSn(questionToSystemSn,receiveBaseId,carId);

                            }
//                            if(questionToSystemSn == rightQuestionSystemSn && dashboardWarningLightsDialog != null && !dashboardWarningLightsDialog.isShowing()){//故障灯
//                                ToastUtils.showMessage(mContext.getString(R.string.light_complete));
//
//                            }


                            return;
                        }else if ( questionToSystemSn == appearanceQuestionSystemSn && finishAmount == totalAmount && isSuccessAppearance) {
                            isSuccessAppearance=false;
                            // 完成
                            //刷新初检信息接口
                            requestDetection(receiveBaseId,carId,false,false);
                            System.out.println("车损外观数据相等");
                            if(questionToSystemSn == appearanceQuestionSystemSn && appearanceamageDialog != null && appearanceamageDialog.isShowing()){//外观损伤
                                appearanceamageDialog.setDetailSystemSn(appearanceQuestionSystemSn,receiveBaseId,carId);

                            }
//                            if(questionToSystemSn == appearanceQuestionSystemSn && appearanceamageDialog != null && !appearanceamageDialog.isShowing()){//外观损伤
//                                ToastUtils.showMessage(mContext.getString(R.string.appearance_complete));
//
//                            }
                            return;
                        }

                            //totalAmount>0   finishAmount!=totalAmount 请求中
                            //totalAmount>0  totalAmount ==finishAmount   完成



                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    /**
     * 展示题目
     * @param mList
     */
    private void showViewList(List<List<DetectionBean>> mList,boolean isShowLoadinglight,boolean isShowLoadingAppearance) {
        carMap = new HashMap<>();
        ll_content.removeAllViews();
        for (List<DetectionBean> sList : mList) {
            if (X3StringUtils.isListEmpty(sList)) {
                continue;
            }
            LinearLayout ll_question = new LinearLayout(mContext);
            LinearLayout.LayoutParams questionLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            questionLayout.topMargin = X3ScreenUtils.dipToPx(12,mContext);
            ll_question.setLayoutParams(questionLayout);
            for (int i = 0; i < sList.size(); i++) {
                DetectionBean detectionBean = sList.get(i);
                if (detectionBean == null) {
                    continue;
                }

                showDetectionItem(sList,i,isShowLoadinglight,isShowLoadingAppearance);
            }

        }
    }

    @Override
    public void dismiss() {
        EventBus.getDefault().unregister(this);
        removeCallbacks();

        super.dismiss();
    }
    private void removeCallbacks(){
        if(handler!=null){
            handler.removeCallbacks(runnable);
            handler=null;
        }
    }
    /**
     * 增添每一项的题目
     * @param sList
     * @param i
     */
    private void showDetectionItem(List<DetectionBean> sList, int i,boolean isShowLoadinglight,boolean isShowLoadingAppearance) {
        DetectionBean detectionBean = sList.get(i);

        List<DetectionBean.QuestionToItemDetectionFirstVOListBean> contentList = detectionBean.questionToItemDetectionFirstVOList;

        View itemView = View.inflate(mContext,R.layout.x3_detection_item,null);
        LinearLayout.LayoutParams itemLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        itemView.setLayoutParams(itemLayout);
        ImageView iv_icon = itemView.findViewById(R.id.iv_icon);
        TextView tv_name_span = itemView.findViewById(R.id.tv_name_span);
        TextView et_car_name = itemView.findViewById(R.id.et_car_name);
        RecyclerView recyclerView=itemView.findViewById(R.id.recyclerview);
        TextView tv_distinguish=itemView.findViewById(R.id.tv_distinguish);
        ProgressBar pb_loading= itemView.findViewById(R.id.pb_loading);

        tv_name_span.setText(detectionBean.toSystem + "");
        String questionToSystemSn = detectionBean.questionToSystemSn;
        String title =detectionBean.toSystem;

        CarDetectionBean carDetectionBean = new CarDetectionBean();
        carDetectionBean.et_car_name = et_car_name;
        carDetectionBean.tv_name_span = tv_name_span;
        carMap.put(questionToSystemSn,carDetectionBean);

        if (X3StringUtils.isListEmpty(contentList)) {
            carDetectionBean.et_car_name.setVisibility(GONE);
            carDetectionBean.et_car_name.setText("");
            carDetectionBean.tv_name_span.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
            carDetectionBean.tv_name_span.setTextColor(Color.parseColor("#333333"));

            if ("sys_thing_001".equals(questionToSystemSn)) {
                carDetectionBean.et_car_name.setVisibility(View.VISIBLE);
                carDetectionBean.et_car_name.setText(mContext.getString(R.string.x3_thine_not_input));
                carDetectionBean.tv_name_span.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                carDetectionBean.tv_name_span.setTextColor(Color.parseColor("#666666"));
            }
        }else {
            String content = "";
            if ("sys_thing_001".equals(questionToSystemSn)) {

                for (DetectionBean.QuestionToItemDetectionFirstVOListBean contentBean : contentList) {
                    List<String> questionList = contentBean.questionList;

                    String replace = questionList.toString().replace("[", "").replace("]", "");
                    if (X3StringUtils.isEmpty(replace)) {
                        continue;
                    }
                    String[] split = replace.split(",");
                    for (int index = 0; index < split.length; index++) {
                        if (X3StringUtils.isEmpty(content)) {
                            content = split[index];
                        }else {
                            content += "\n" + split[index];
                        }
                    }
                }

            }else {
                for (DetectionBean.QuestionToItemDetectionFirstVOListBean contentBean : contentList) {
                    String itemName = X3StringUtils.isEmpty(contentBean.itemName) ? "" : contentBean.itemName + " ";
                    List<String> questionList = contentBean.questionList;

                    String replace = questionList.toString().replace("[", "").replace("]", "");

                    if (contentList.indexOf(contentBean) == contentList.size() - 1) {
                        content += itemName + replace;
                    }else {
                        content += itemName + replace + "\n";
                    }
                }
            }

            if (X3StringUtils.isEmpty(content)) {
                carDetectionBean.et_car_name.setVisibility(GONE);
                carDetectionBean.et_car_name.setText("");
                carDetectionBean.tv_name_span.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
                carDetectionBean.tv_name_span.setTextColor(Color.parseColor("#333333"));

                if ("sys_thing_001".equals(questionToSystemSn)) {
                    carDetectionBean.et_car_name.setVisibility(View.VISIBLE);
                    carDetectionBean.et_car_name.setText(mContext.getString(R.string.x3_thing_not));
                    carDetectionBean.tv_name_span.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    carDetectionBean.tv_name_span.setTextColor(Color.parseColor("#666666"));
                }
            }else {
                carDetectionBean.et_car_name.setVisibility(View.VISIBLE);
                carDetectionBean.et_car_name.setText(content);
                carDetectionBean.tv_name_span.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                carDetectionBean.tv_name_span.setTextColor(Color.parseColor("#666666"));
            }
        }

        checkEmpty();

        if ("sys_oil_001".equals(questionToSystemSn)) {
            iv_icon.setImageResource(R.mipmap.x3_car_oil_icon);
            if (carDetectionBean.et_car_name.getVisibility() == View.VISIBLE) {
                oilContent = carDetectionBean.et_car_name.getText().toString();
            }
            itemView.setOnClickListener(v -> showClickOil(questionToSystemSn));

        }else if ("sys_tyre_001".equals(questionToSystemSn)) {
            iv_icon.setImageResource(R.mipmap.x3_car_tyre_icon);
            itemView.setOnClickListener(v -> showClickTyre(questionToSystemSn,detectionBean.toSystem));

        }else if ("sys_paint_001".equals(questionToSystemSn)) {
            iv_icon.setImageResource(R.mipmap.x3_car_paint_icon);
            itemView.setOnClickListener(v -> showClickPaint(questionToSystemSn,detectionBean.toSystem));

        }else if("sys_thing_001".equals(questionToSystemSn)){
            // 物品清单
            iv_icon.setImageResource(R.mipmap.x3_car_thing_icon);
            itemView.setOnClickListener(v -> showClickThing(questionToSystemSn));

        }else if ("sys_remark_001".equals(questionToSystemSn)) {
            iv_icon.setImageResource(R.mipmap.x3_car_owner_text_icon);
            itemView.setOnClickListener(v -> showClickText(questionToSystemSn,detectionBean.toSystem));
        }else if("sys_light_001".equals(questionToSystemSn)){//故障灯
            rightQuestionSystemSn = questionToSystemSn;
            carDetectionBean.et_car_name.setVisibility(GONE);
            //是否显示  请求中
            if(isShowLoadinglight) {
                recyclerView.setVisibility(GONE);
                tv_distinguish.setVisibility(View.VISIBLE);
                pb_loading.setVisibility(View.VISIBLE);
            }else {
                recyclerView.setVisibility(View.VISIBLE);
                tv_distinguish.setVisibility(GONE);
                pb_loading.setVisibility(GONE);
            }

            GridLayoutManager gridLayoutManager= new GridLayoutManager(mContext,6,GridLayoutManager.VERTICAL,false){
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.addItemDecoration(new X3SpaceItemDecoration(X3ScreenUtils.dipToPx(18,mContext)));
            X3RecongitionWarnPhotoAdapter recongitionWarnPhotoAdapter = new X3RecongitionWarnPhotoAdapter(mContext);
            recyclerView.setAdapter(recongitionWarnPhotoAdapter);
            recyclerView.setLayoutFrozen(true);
            List<String> list=new ArrayList<>();
            list.clear();

            if (!X3StringUtils.isListEmpty(contentList)) {
                recyclerView.setVisibility(View.VISIBLE);
                for (DetectionBean.QuestionToItemDetectionFirstVOListBean contentBean : contentList) {
                    List<String> questionList = contentBean.questionList;
                    for(int j=0;j<questionList.size();j++){
                        String s = questionList.get(j);
                        String substring = s.substring(0, s.indexOf("_"));
                        list.add(substring);
                    }

                }
                recongitionWarnPhotoAdapter.setList(list);
            }else {
                recyclerView.setVisibility(GONE);
            }


            iv_icon.setImageResource(R.mipmap.x3_dashwarnlights);
            itemView.setOnClickListener(v -> showClickLight(questionToSystemSn,receiveBaseId,title));
            //初检车辆图片识别状态查询

        }else if ("sys_feature_001".equals(questionToSystemSn)) {//外观损伤
            appearanceQuestionSystemSn = questionToSystemSn;

            //是否显示  请求中
            if(isShowLoadingAppearance) {
                carDetectionBean.et_car_name.setVisibility(GONE);
                tv_distinguish.setVisibility(View.VISIBLE);
                pb_loading.setVisibility(View.VISIBLE);
            }else {
                if(X3StringUtils.isListEmpty(contentList)){
                    carDetectionBean.et_car_name.setVisibility(GONE);
                }else {
                    carDetectionBean.et_car_name.setVisibility(View.VISIBLE);
                }

                tv_distinguish.setVisibility(GONE);
                pb_loading.setVisibility(GONE);
            }


            iv_icon.setImageResource(R.mipmap.x3_appearancedamage);
            itemView.setOnClickListener(v -> showClickAppearance(questionToSystemSn,receiveBaseId,title));
        } else {

            iv_icon.setImageResource(R.mipmap.x3_car_icon_no);
            itemView.setOnClickListener(null);

        }

        ll_content.addView(itemView);

        View lineView = new View(mContext);
        LinearLayout.LayoutParams lineLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, X3ScreenUtils.dipToPx(1,mContext));
        lineLayout.leftMargin = X3ScreenUtils.dipToPx(16,mContext);
        lineView.setLayoutParams(lineLayout);
        lineView.setBackgroundColor(Color.parseColor("#F7F7FA"));
        ll_content.addView(lineView);
    }

    private void showClickAppearance(String questionToSystemSn, int receiveBaseId,String title) {//外观损伤
        if (appearanceamageDialog == null) {
            appearanceamageDialog = new X3AppearanceamageDialog(mContext);
        }
        appearanceamageDialog.setDialogTitle(title);
        appearanceamageDialog.show();
        appearanceamageDialog.setDetailSystemSn(questionToSystemSn,receiveBaseId,carId);
    }

    //故障灯
    private void showClickLight(String questionToSystemSn, int receiveBaseId, String title) {
        if (dashboardWarningLightsDialog == null) {
            dashboardWarningLightsDialog = new X3DashboardWarningLightsDialog(mContext);
        }
        dashboardWarningLightsDialog.setDialogTitle(title);
        dashboardWarningLightsDialog.show();
        dashboardWarningLightsDialog.from(1);
        dashboardWarningLightsDialog.setDetailSystemSn(questionToSystemSn,receiveBaseId,carId);
    }

    /**
     * 物品清单
     */
    private void showClickThing(String questionToSystemSn) {
        if (!X3Utils.isFastClick()) {
            return;
        }
        CarDetectionBean carDetectionBean = carMap.get(questionToSystemSn);
        X3BottomAloneThingDialog thingDialog = new X3BottomAloneThingDialog(mContext);
        thingDialog.show();
        String thing = carDetectionBean.et_car_name.getText().toString();
        String s = thing.replaceAll("\n", ",");
        thingDialog.setQuestionSystemSn(s,receiveBaseId);
        thingDialog.setiOnRefresh(this);
    }

    /**
     * 车漆
     * @param questionToSystemSn
     * @param toSystem
     */
    private void showClickPaint(String questionToSystemSn, String toSystem) {
        if (!X3Utils.isFastClick()) {
            return;
        }
        X3BottomHomeCarPaintDialog carPaintDialog = new X3BottomHomeCarPaintDialog(mContext);
        carPaintDialog.show();
        carPaintDialog.setQuestionSystemSn(questionToSystemSn,receiveBaseId);
        carPaintDialog.setiOnRefresh(this);
    }

    /**
     * 点击轮胎/轮毂
     * @param questionToSystemSn
     */
    private void showClickTyre(String questionToSystemSn,String title) {
        if (!X3Utils.isFastClick()) {
            return;
        }
        X3BottomHomeTyreDialog tyreDialog = new X3BottomHomeTyreDialog(mContext);
        tyreDialog.show();
        tyreDialog.setQuestionSystemSn(questionToSystemSn,receiveBaseId);
        tyreDialog.setiOnRefresh(this);
    }

    /**
     * 点击车主备注
     */
    private void showClickText(String questionToSystemSn,String title) {
        if (!X3Utils.isFastClick()) {
            return;
        }
        CarDetectionBean carDetectionBean = carMap.get(questionToSystemSn);
        if (carOwnerTextDialog == null) {
            carOwnerTextDialog = new X3BottomHomeCarOwnerTextDialog(mContext);
        }
        carOwnerTextDialog.show();
        carOwnerTextDialog.setContentText(carDetectionBean.et_car_name.getText().toString());
        carOwnerTextDialog.showReceiveBaseId(receiveBaseId,title);
        carOwnerTextDialog.setiOnRefresh(this);
    }

    /**
     * 点击进厂油量
     */
    private void showClickOil(String questionToSystemSn) {
        if (!X3Utils.isFastClick()) {
            return;
        }
        if (X3StringUtils.isEmpty(oilContent)) {
            oilContent = "100";
        }
        CarDetectionBean carDetectionBean = carMap.get(questionToSystemSn);

        X3BottomPrecentPickerDialog precentPickerDialog = new X3BottomPrecentPickerDialog(mContext);
        precentPickerDialog.show();
        precentPickerDialog.showContent(oilContent,oilList,carDetectionBean.tv_name_span.getText().toString());
        precentPickerDialog.setiOnItemCameraClick(new X3BottomPrecentPickerDialog.IOpenNumberClick() {
            @Override
            public void iOpenCancelListener() {

            }

            @Override
            public void iOpenAffirmListener(String selectData) {
                oilContent = selectData;
                saveOrUpdate(selectData);
            }
        });
    }

    /**
     * 更新进厂油量
     * @param selectData
     */
    private void saveOrUpdate(String selectData) {
        if (X3StringUtils.isEmpty(selectData)) {
            return;
        }
        if (!X3StringUtils.isNum(selectData)) {
            return;
        }
        NetWorkHttp.getApi().saveOrUpdateOil(Integer.parseInt(selectData),receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        iOnRefreshData();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_next || id == R.id.tv_cancel) {
            dismiss();
            if (iOnClick != null) {
                iOnClick.iOnHomeAloneCarNext();
            }
        } else if (id == R.id.tv_clear) {/**
         * 清空所选数据
         */
            if (carMap == null) {
                tv_clear.setVisibility(GONE);
                return;
            }

            for (String key : carMap.keySet()) {
                CarDetectionBean carDetectionBean = carMap.get(key);
                if (carDetectionBean == null) {
                    continue;
                }
                carDetectionBean.et_car_name.setText("");
                carDetectionBean.tv_name_span.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                carDetectionBean.tv_name_span.setTextColor(Color.parseColor("#333333"));
                carDetectionBean.et_car_name.setVisibility(GONE);
            }

            tv_clear.setVisibility(GONE);
        }
    }

    private void checkEmpty() {
//        if (carMap == null) {
//            tv_clear.setVisibility(GONE);
//            return;
//        }
//        for (String key : carMap.keySet()) {
//            CarDetectionBean carDetectionBean = carMap.get(key);
//            if (carDetectionBean.et_car_name.getVisibility() == View.VISIBLE) {
//                tv_clear.setVisibility(View.VISIBLE);
//                break;
//            }
//        }
    }

    IOnClickCar iOnClick;

    public void setiOnClick(IOnClickCar iOnClick) {
        this.iOnClick = iOnClick;
    }

    @Override
    public void iOnRefreshData() {
        requestDetection(receiveBaseId,carId,false,false);
    }

//    @Override
//    public void iOnClickSelect(String year, String month) {
//        boolean zhRCN = LanguageUtiles.isZhRCN();
//        if (zhRCN) {
//            et_car_paint.setText(year + "-" + month);
//        }else {
//            et_car_paint.setText(month + ", " + year);
//        }
//        et_car_paint.setVisibility(View.VISIBLE);
//        tv_car_paint_span.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//        tv_car_paint_span.setTextColor(Color.parseColor("#666666"));
//        tv_clear.setVisibility(View.VISIBLE);
//    }

    public interface IOnClickCar {

        /**
         * @param
         */
        void iOnHomeAloneCarNext();
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageStart("初检信息弹窗");
    }
}
